<div class="form wide">

    <?php
    /**
     * @var $form CActiveForm
     * @var $this BackOfficeController
     */
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'document-form',
        'enableClientValidation' => true,
        'enableAjaxValidation' => false,
        'clientOptions' => array(
            'validateOnSubmit' => true,
            'enctype' => 'multipart/form-data',
            'afterValidate'=>"js:function(form,data,hasError){
                $('ul.ui-tabs-nav li a').removeClass('error');
                if (hasError)
                {
                    $('#document-form div.row.error').each(function(index, value){
                        var tab_id = $(this).parents('div.ui-tabs-panel').attr('id');
                        $('ul.ui-tabs-nav li a[title=#'+tab_id+']').addClass('error');    
                    });
                    return false;   
                }
                else
                    return true;
            }"
        )
    )); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php
    $langTab = array();
    $langList = Language::model()->findAll('status=1');
    if (count($langList)) {
        foreach ($langList as $i => $lang) {
            $this->beginClip($lang->name);
            if ($model->isNewRecord) {
                $languageDocument = new LanguageDocument();
                $languageDocument->locale = $lang->locale;
            }
            else {
                $languageDocument = LanguageDocument::model()->findByAttributes(array(
                    'document_id' => $model->id,
                    'locale' => $lang->locale,
                ));
                if (!$languageDocument) {
                    $languageDocument = new LanguageDocument();
                    $languageDocument->locale = $lang->locale;
                }
            }
            $this->renderPartial('Site.views.admin.languageDocument._form', array(
                'model' => $languageDocument,
                'form' => $form,
                'i' => $i,
            ));
            $this->endClip();
            $langTab[$lang->name] = $this->Clips[$lang->name];
        }

        if (count($langTab)) {
            $this->widget('zii.widgets.jui.CJuiTabs', array(
                'id' => 'language-tabs',
                'tabs' => $langTab,
            ));
        }
    }
    ?>


    <?php $this->beginClip('general');?>
    <?php if (!$model->IsNewRecord) echo $form->hiddenField($model, "id"); ?>
    <div class="row">
        <?php echo $form->labelEx($model, 'file_upload'); ?>
        <?php
        $this->widget('Admin.components.FileWidget', array(
            'name' => CHtml::resolveName($model, $attribute = 'file_upload'),
            'value' => $model->file_upload,
            'root' => Settings::DOCS_PATH,
        ));
        if (!$model->isNewRecord) :
        ?>
            <a href="<?php echo $this->createUrl('/Site/admin/document/download',array('id'=>$model->id)) ?>"> | Download</a>
        <?php endif; ?>
        <?php echo $form->error($model, 'file_upload'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'category_id'); ?>
        <?php
        $this->widget('CategoryDropDownList', array(
            'model' => $model,
            'attribute' => 'category_id',
            'rootId' => SiteSettings::ROOT_DOCUMENT_CATEGORY_ID,
            'includeRoot' => false,
            'htmlOptions' => array(
                'prompt' => '-- Choose The Category --',
            ),
        ));
        ?>
        <?php echo $form->error($model, 'category_id'); ?>
    </div>
    
    <div class="row">
        <?php echo $form->labelEx($model, 'status'); ?>
        <?php echo $form->dropDownList($model, 'status', $model->getStatusOptions()); ?>
        <?php echo $form->error($model, 'status'); ?>
    </div>

    <?php if (!$model->isNewRecord) : ?>
    <div class="row">
        <?php echo $form->labelEx($model, 'create_time'); ?>
        <?php echo Yii::app()->getDateFormatter()->formatDateTime($model->create_time); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'update_time'); ?>
        <?php echo Yii::app()->getDateFormatter()->formatDateTime($model->update_time); ?>
    </div>
    <?php endif;?>
    <?php $this->endClip();?>

    <?php
    $tabs = array(
        'General' => $this->Clips['general'],
    );
    $this->widget('zii.widgets.jui.CJuiTabs', array(
        'id' => 'document-tabs',
        'tabs' => $tabs,
    ));
    ?>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

    <?php $this->endWidget(); ?>

</div><!-- form -->